#include <bits/stdc++.h>
using namespace std;

void work() {
  int n, q;
  cin >> n >> q;
  vector<int> a(n + 1, 0);
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  sort(a.begin() + 1, a.begin() + n + 1, greater<int>());

  vector<int> s(n + 2, 0);
  for (int i = 1; i <= q; i++) {
    int x, y;
    cin >> x >> y;
    s[x] += 1;
    s[y + 1] -= 1;
  }
  vector<int> ss(n + 1, 0);
  for (int i = 1; i <= n; i++) {
    ss[i] = ss[i - 1] + s[i];
  }

  sort(ss.begin() + 1, ss.begin() + n + 1, greater<int>());

  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += (long long)a[i] * ss[i];
  }
  cout << ans << endl;
}
int main() {
  int T;
  cin >> T;
  for (int i = 1; i <= T; i++) {
    work();
  }
  return 0;
}